/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.resources.joram.wrapper;

import java.util.Properties;

import javax.jms.TopicConnectionFactory;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;
import org.objectweb.fractal.api.control.LifeCycleController;
import org.objectweb.fractal.util.Fractal;
import org.objectweb.joram.client.jms.admin.AdminException;
import org.objectweb.joram.client.jms.admin.AdminModule;
import org.objectweb.joram.client.jms.admin.User;
import org.objectweb.joram.client.jms.tcp.TopicTcpConnectionFactory;

import fr.jade.base.JadeBaseComponent;
import fr.jade.fraclite.api.control.NoSuchAttributeException;
import fr.jade.resources.joram.api.UserInterface;
import fr.jade.util.FractalNavigator;
import fr.jade.util.FractalUtil;
import fr.jade.util.JadeException;


public class UserWrapper extends JadeBaseComponent implements UserInterface, LifeCycleController{

	private String listAtt[] = {"name", "pass"};

	private String name;

	private String pass;

	private User myUser;
	
	private boolean started;
	
	private Properties serverConfig;
	
	public User getUser() {
		return myUser;
	}
	
	private void initServerConfig() throws NoSuchInterfaceException, AdminException, NoSuchAttributeException, JadeException{
		serverConfig = new Properties();
		Component server = JoramUtils.getServerComponent(self);
//		serverConfig.put("hostName", FractalUtil.getAttributeController(server).getAttribute("host"));
		String hostname = FractalUtil.getAttributeController(FractalNavigator.getNode(server)).getAttribute("hostName");
		
		serverConfig.put("hostName", hostname);
		serverConfig.put("port", JoramUtils.PORT_NUMBER_START + Integer.parseInt(FractalUtil.getAttributeController(server).getAttribute("sid")));
		serverConfig.put("sid",FractalUtil.getAttributeController(server).getAttribute("sid"));
		serverConfig.put("userAdmin", FractalUtil.getAttributeController(server).getAttribute("userAdmin"));
		serverConfig.put("passAdmin", FractalUtil.getAttributeController(server).getAttribute("passAdmin"));
	}			

	public String getAttribute(String attName) throws NoSuchAttributeException {
		if (attName.equals("name")){
			return name;
		} else if (attName.equals("pass")){
			return pass;
		} else{
			return super.getAttribute(attName);
		}
	}

	public String[] listFcAtt() {
		return super.concat(listAtt, super.listFcAtt());
	}

	public void setAttribute(String attName, String attValue) throws NoSuchAttributeException {
		if (attName.equals("name")){
			name = attValue;
		}else if (attName.equals("pass")){
			pass = attValue;
		}else{
			super.setAttribute(attName, attValue);
		}
	}

	public String getFcState() {
		return started ? STARTED : STOPPED;
	}

	public void startFc() throws IllegalLifeCycleException {
		if(!started && JoramUtils.serverIsStarted(self)){
  	  try {
  			initServerConfig();	
  			TopicConnectionFactory con = TopicTcpConnectionFactory.create(serverConfig.getProperty("hostName"), JoramUtils.PORT_NUMBER_START + Integer.parseInt(serverConfig.getProperty("sid")));
  			AdminModule.connect(con, serverConfig.getProperty("userAdmin"), serverConfig.getProperty("passAdmin"));
  			myUser = User.create(name, pass, Integer.parseInt(serverConfig.getProperty("sid")));
  			log("User " + Fractal.getNameController(self).getFcName() + " created");
  			AdminModule.disconnect();
  			started = true;
  		} catch (Exception e) {
  		  log(e);
  			throw new IllegalLifeCycleException("Error while starting the user wrapper");
  		} 
		}
	}

  public void stopFc() throws IllegalLifeCycleException {
	  if(started){
  		try {
  			TopicConnectionFactory con = TopicTcpConnectionFactory.create(serverConfig.getProperty("hostName"), JoramUtils.PORT_NUMBER_START + Integer.parseInt(serverConfig.getProperty("sid")));
  			AdminModule.connect(con, serverConfig.getProperty("userAdmin"), serverConfig.getProperty("passAdmin"));
  			myUser.delete();
  			AdminModule.disconnect();
  			started = false;
  		} catch (Exception e) {
  			log(e);
  			throw new IllegalLifeCycleException("Error while starting the user wrapper");
  		}
	  }
	}

	
}
